package com.xl.web.controller.common;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.xl.core.util.HtmlUtil;
import com.xl.base.web.BaseController;
import com.xl.web.entity.area.Area;
import com.xl.web.service.area.AreaService;

/**
 * 省市区获取控制器
 * 
 * @author 梁华
 */
@Controller
@Scope("prototype")
@RequestMapping("/area")
public class AreaController extends BaseController {

	// 注入省市区管理service
	@Autowired(required = false)
	private AreaService<Area> areaService;

	/**
	 * 测试省市区三级联动页面
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/areaModel")
	public ModelAndView addType() throws Exception {

		Map<String, Object> context = getRootMap(request);

		context.put("provinceId", "140000");
		context.put("cityId", "140200");
		context.put("areaId", "140211");
		return forword("area/area_model", context);
	}

	/**
	 * 获取省市区列表
	 * 
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("/listArea")
	public void upload(Area area) throws Exception {

		// 根据给定条件获取到省市区列表
		List<Area> areaList = areaService.getDictList(area);

		// 如果获取的数据为空，则new一个对象，防止页面出错
		if (areaList == null || areaList.size() == 0) {
			areaList = new ArrayList<Area>();
		}

		HtmlUtil.writerJson(response, areaList);
	}
}
